package lib;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class OneMenuHasClickFilter extends WebFilter{

	@Override
	public void lay_noi_dung_web(String url, String str1, String str2) {
		// TODO Auto-generated method stub
		ConnectionWeb con = new ConnectionWeb();
		String ch1="";
		String ch2="";
		String link="";
		int t_chiso=0;
		
		//str1=StringEdit.EditXpath(str1);
		//str2=StringEdit.EditXpath(str2);
		// do parsing
		Document root;
		root = con.getInfo(url);
		List<Element> nodes=con.executeXpath(root, str1,1);	
			
		if(nodes.size()>0)
		{
			Element tr = nodes.get(0);
		//	System.out.println(tr.getText().toString());
			
			List<?> lNode= tr.getElementsByTag("a");
			for(int i= 0; i< lNode.size();i++)
			{
				TagNode t1= (TagNode) lNode.get(i);
				ch1=t1.getText().toString();
				t_chiso= i;

				String tlink=StringEdit.EditLink(url,t1.getAttributeByName("href"));
				tlink= StringEdit.EditLink(url, tlink);
				Document root2 =con.getInfo(tlink);
				
				List<Element> ln =con.executeXpath(root2, str1, 1); 
				if(ln.size()>0)
				{
					Element temp = ln.get(0);

					List lt2= temp.getElementsByTag("a");
					if(lt2.size()==lNode.size())
					{	System.out.println(i);
							ch2= StringEdit.EditDir(ch2);
							this.setLink(ch2, link);
						}
						else
						{	System.out.println(i);
							int num1= lt2.size()-lNode.size();
							for(int j= t_chiso+1; j<t_chiso+ num1+1;j++)
							{
								TagNode t3= (TagNode) lt2.get(j);
									ch2="";
								 ch2=ch1+"/"+t3.getText();
								 link =StringEdit.EditLink(url,t3.getAttributeByName("href"));
								 link=StringEdit.EditLink(url, link);
								ch2= StringEdit.EditDir(ch2);
								this.setLink(ch2, link);
							}
						}
					}								
			}
}
	}
	
	public void lay_noi_dung_web2(String url, String str1, String str2) {
		// TODO Auto-generated method stub
		ConnectionWeb con = new ConnectionWeb();
		String ch1="";
		String ch2="";
		String link="";
		int t_chiso=0;
		
		//str1=StringEdit.EditXpath(str1);
		//str2=StringEdit.EditXpath(str2);
		// do parsing
		Document root;
				root = con.getInfo(url);
				List<Element> nodes=con.executeXpath(root, str1,1);
				for(int chiso=0;chiso<nodes.size();chiso++)
				{
					Element tr = nodes.get(chiso);
					System.out.println(tr.text());
					
					List<?> lNode= tr.getElementsByTag("a");
					for(int i= 0; i< lNode.size();i++)
					{
						Element t1= (Element) lNode.get(i);
						ch1=t1.text();
						t_chiso= i;

						String tlink=StringEdit.EditLink(url,t1.attr("href"));
						tlink= StringEdit.EditLink(url, tlink);
						Document root2 =con.getInfo(tlink);
						
						List<Element> ln = con.executeXpath(root2, str1,0);
						for(int chiso2=chiso;chiso2<ln.size();chiso2++)
						{
							Element temp = ln.get(chiso2);
			
							List<Element> lt2= temp.getElementsByTag("a");
							if(lt2.size()==lNode.size())
							{	
									ch1= StringEdit.EditDir(ch1);
									 link=StringEdit.EditLink(url, tlink);
									this.setLink(ch1, link);
							}
							else
							{	System.out.println(i);
									int num1= lt2.size()-lNode.size();
									for(int j= t_chiso+1; j<t_chiso+ num1+1;j++)
									{
										Element t3= (Element) lt2.get(j);
											ch2="";
										 ch2=ch1+"/"+t3.text();
										 link =StringEdit.EditLink(url,t3.attr("href"));
										 link=StringEdit.EditLink(url, link);
										ch2= StringEdit.EditDir(ch2);
										this.setLink(ch2, link);
									}
							}
							}	

					}
			}					

	}
}
